<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="openid" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
        >
          <el-option label="页面" :value="1"></el-option>
          <el-option label="商品详情" :value="2"></el-option>
          <el-option label="添加购物车" :value="3"></el-option>
          <el-option label="下单" :value="4"></el-option>
          <el-option label="支付" :value="5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属省份" prop="city">
        <el-select
          v-model="queryParams.province"
          placeholder="请选择所属省份"
          clearable
        >
          <el-option label="河北省" value="河北省"></el-option>
          <el-option label="其他省份" value="其他省份"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属城市" prop="city">
        <el-select
          v-model="queryParams.city"
          placeholder="请选择所属城市"
          clearable
        >
          <el-option label="石家庄市" value="石家庄市"></el-option>
          <el-option label="唐山市" value="唐山市"></el-option>
          <el-option label="邯郸市" value="邯郸市"></el-option>
          <el-option label="秦皇岛市" value="秦皇岛市"></el-option>
          <el-option label="保定市" value="保定市"></el-option>
          <el-option label="张家口市" value="张家口市"></el-option>
          <el-option label="承德市" value="承德市"></el-option>
          <el-option label="廊坊市" value="廊坊市"></el-option>
          <el-option label="沧州市" value="沧州市"></el-option>
          <el-option label="衡水市" value="衡水市"></el-option>
          <el-option label="邢台市" value="邢台市"></el-option>
          <el-option label="雄安" value="雄安"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
          v-model="range"
          type="datetimerange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cancel:traceLogs:export']"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
      <div class="total_all">总条数:{{ total }}</div>
    </el-row>

    <el-table v-loading="loading" :data="logsList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="openid" align="center" prop="openid" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">页面</el-tag>
          <el-tag type="success" v-else-if="scope.row.type == 2"
            >商品详情</el-tag
          >
          <el-tag type="danger" v-else-if="scope.row.type == 3"
            >添加购物车</el-tag
          >
          <el-tag type="warning" v-else-if="scope.row.type == 4">下单</el-tag>
          <el-tag type="success" v-else-if="scope.row.type == 5">支付</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="ip地址" align="center" prop="createdIp" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <!-- <el-table-column label="区县" align="center" prop="area" /> -->
      <el-table-column
        label="浏览时间"
        align="center"
        prop="createdAt"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listLogs } from "@/api/frontUser/logs";

export default {
  name: "Logs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      range: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户足迹表格数据
      logsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: null,
        productName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户足迹列表 */
    getList() {
      const [startTime, endTime] = this.range;
      this.queryParams.startTime = startTime;
      this.queryParams.endTime = endTime;
      this.loading = true;
      listLogs(this.queryParams).then((response) => {
        this.logsList = [...response.rows].map((item) => {
          item.province = item.province === "[]" ? "" : item.province;
          item.city = item.city === "[]" ? "" : item.city;
          return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mallId: null,
        userId: null,
        type: null,
        linkId: null,
        nextLinkId: null,
        createdAt: null,
        updatedAt: null,
        createdIp: null,
        province: null,
        city: null,
        area: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "cancel/traceLogs/export",
        {
          ...this.queryParams,
        },
        `logs_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.mb8 {
  overflow: hidden;
}
.total_all {
  position: relative;
  float: right;
  height: 28px;
  line-height: 28px;
  margin-right: 20px;
}
</style>
